package com.boot.meta;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Chunsheng.Zhang
 * @date 2021/1/15 11:38
 * @description : TODO
 */
@Component
public class InitMetaObjectHandler implements MetaObjectHandler {



    //你需要填充的字段
    private final static String INSERT_DATE = "insertDate";
    private final static String UPDATE_DATE = "updateDate";
    private final static String ISDELETE = "isDelete";
    private final static String STATUS = "status";




    @Override
    public void insertFill(MetaObject metaObject) {
        Object insertDate = getFieldValByName(INSERT_DATE, metaObject);
        Object updateDate = getFieldValByName(UPDATE_DATE, metaObject);
        Object isDelete = getFieldValByName(ISDELETE, metaObject);
        Object status = getFieldValByName(STATUS, metaObject);

        Date currentDate = new Date();

        if (insertDate == null) {
            setFieldValByName(INSERT_DATE, currentDate, metaObject);
        }
        if (updateDate == null) {
            setFieldValByName(UPDATE_DATE, currentDate, metaObject);
        }

        if (isDelete == null) {
            setFieldValByName(ISDELETE, 1, metaObject);
        }


        if (status == null) {
            setFieldValByName(STATUS, 0, metaObject);
        }
    }

    /**
     * 更新填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        /*Integer userId = JwtUtils.getUserId();
        String userName = JwtUtils.getUserName();*/

        setFieldValByName(UPDATE_DATE, new Date(), metaObject);
    }
}
